package com.biz.ssm.service.impl;

import com.biz.ssm.base.BaseServiceImpl;
import com.biz.ssm.domain.Role;
import com.biz.ssm.domain.RoleAuthority;
import com.biz.ssm.mapper.RoleAuthorityMapper;
import com.biz.ssm.mapper.RoleMapper;
import com.biz.ssm.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 *
 * RoleService
 *
 **/
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, Long ,RoleMapper> implements RoleService{

	@Resource
	private RoleMapper roleMapper;

	@Resource
	private RoleAuthorityMapper roleAuthorityMapper;

	@Override
	public RoleMapper getMapper() {
		return roleMapper;
	}

	@Override
	public List<Role> getRolesByAdmin(Long adminId) {
		return roleMapper.getRolesByAdmin(adminId);
	}

	@Override
	public List<Role> findListByIds(Long[] ids) {
		return roleMapper.findListByIds(ids);
	}

	@Override
	@Transactional
	public Boolean modifyRole(Role role) {
		try {
			roleMapper.update(role);
			List<String> list = role.getAuthorities();
			List<String> pList = roleAuthorityMapper.getAuthoritysByRole(role.getId());
			/** 增加权限 **/
			if (list.size() >= pList.size()) {
				for (int i = 0; i < list.size(); i++) {
					if (!pList.contains(list.get(i))) {
						/** 若原先不存在，则新增 **/
						RoleAuthority roleAuthority = new RoleAuthority();
						roleAuthority.setRole(role.getId());
						roleAuthority.setAuthorities(list.get(i));
						roleAuthorityMapper.saveAuthority(roleAuthority);
					}
				}
				for (int i = 0; i < pList.size(); i++) {
					if (!list.contains(pList.get(i))) {
						/** 若原先不存在，则新增 **/
						roleAuthorityMapper.deleteAuthority(role.getId(), pList.get(i));
					}
				}
			}
			/** 删除权限 **/
			if (list.size() < pList.size()) {
				for (int i = 0; i < pList.size(); i++) {
					if (!list.contains(pList.get(i))) {
						/** 若现在不存在，则删除 **/
						roleAuthorityMapper.deleteAuthority(role.getId(), pList.get(i));
					}
				}
				for (int i = 0; i < list.size(); i++) {
					if (!pList.contains(list.get(i))) {
						RoleAuthority roleAuthority = new RoleAuthority();
						roleAuthority.setRole(role.getId());
						roleAuthority.setAuthorities(list.get(i));
						roleAuthorityMapper.saveAuthority(roleAuthority);
					}
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	@Transactional
	public Boolean createRole(Role role) {
		try {
			roleMapper.insert(role);
			for (int i = 0; i < role.getAuthorities().size(); i++) {
				RoleAuthority roleAuthority = new RoleAuthority();
				roleAuthority.setRole(role.getId());
				roleAuthority.setAuthorities(role.getAuthorities().get(i));
				roleAuthorityMapper.saveAuthority(roleAuthority);
			}
		} catch (RuntimeException e) {
			return false;
		}
		return true;
	}

	@Override
	@Transactional
	public void delRole(Role role) {
		roleMapper.deleteById(role.getId());
		this.roleAuthorityMapper.delByRole(role.getId());
	}
}
